package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.manager.redis.RedisManager;
import com.zmn.oms.business.interfaces.messageV1.SendOrderMessageService;
import com.zmn.oms.business.interfaces.messageV1.wechat.MessageWeChatTemplateService;
import com.zmn.oms.common.constant.*;
import com.zmn.oms.common.dto.QueueOrderNodeNotifyDTO;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.messageV1.SendMessageRespDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述： 发送订单消息通知队列
 *
 * @author yule
 * @date 2020/12/14 20:18
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class OrderSendMessageNodeListener {

    private static final Logger logger = LoggerFactory.getLogger(OrderSendMessageNodeListener.class);

    @Autowired
    private SendOrderMessageService sendOrderMessageService;
    @Autowired
    private MessageWeChatTemplateService messageWeChatTemplateService;
    @Resource
    private RedisManager redisManager;

    @AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_ORDER_SEND_MESSAGE_NODE_MQ_TAGS)
    public void sendOrderNodeMessage(String message) {

        logger.info("#oms#send_message# 收到订单消息通知消费[{}]", message);
        if (StringUtil.isBlank(message)) {
            return;
        }
        String orderId = "";
        try {
            SendMessageRespDTO sendMessageRespDTO = JSON.parseObject(message, SendMessageRespDTO.class);
            orderId = Optional.ofNullable(sendMessageRespDTO.getOrderId()).orElse(0L).toString();

            boolean isSendMessage = sendOrderMessageService.sendOrderMessage(sendMessageRespDTO);

            logger.info("#oms#message#orderId={} task 发送工单节点={}，消息结果：{}", orderId, sendMessageRespDTO.getNodeMark(), isSendMessage);
        } catch (Exception ex) {
            logger.error(String.format("#oms#message#orderId=%s，task 发送订单消息结果失败:%s", orderId, ex.getMessage()), ex);
        }
    }

    @AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_TAG_OMS_ORDER_SEND_MESSAGE_QUESTIONNAIRE)
    public void sendQuestionnaireMessage(String message) {
        logger.info("#oms#send_message# 收到订单消息通知消费-发送调查问卷[{}]", message);
        if (StringUtil.isBlank(message)) {
            return;
        }

        String orderId = null;
        try {
            SendMessageRespDTO sendMessageRespDTO = JSON.parseObject(message, SendMessageRespDTO.class);
            orderId = Optional.ofNullable(sendMessageRespDTO.getOrderId()).orElse(0L).toString();
            // 发送微信消息
            messageWeChatTemplateService.sendDesignatedWeChatTemplate(sendMessageRespDTO.getOrderId(), sendMessageRespDTO.getWorkId(), MessageConsts.ZMN_MSG_RULEID_QUESTIONNAIRE);
        } catch (Exception ex) {
            logger.error(String.format("#oms#message#orderId=%s，task 发送调查问卷 失败:%s", orderId, ex.getMessage()), ex);
        }
    }



    // 改约 2022-03-23 不发了
//    @AliyunMQHandler(tags = MessageQueueTagConsts.ORDER_UPDATE_DUTYTIME)
//    public void updateDutytime(String message) {
//
//        logger.info("#oms#send_message# 收到订单改约消息通知消费[{}]", message);
//        if (StringUtil.isBlank(message)) {
//            return;
//        }
//        String orderId = "";
//        try {
//            QueueOrderNodeNotifyDTO queueOrderNodeNotifyDTO = JSON.parseObject(message, QueueOrderNodeNotifyDTO.class);
//            orderId = Optional.ofNullable(queueOrderNodeNotifyDTO.getOrderId()).orElse(0L).toString();
//
//            // 非工程师改约，不发送消息提醒
//            if (!Objects.equals(queueOrderNodeNotifyDTO.getOperatorType(), GlobalConsts.OPERATE_USER_TYPE_MASTER)) {
//                return;
//            }
//
//            SendMessageRespDTO sendMessageRespDTO = new SendMessageRespDTO();
//            sendMessageRespDTO.setNodeMark(MessageRuleIdConsts.MESSAGE_GLOBAL_UPDATE_DUTYTIME_NODE_MARK);
//            sendMessageRespDTO.setLogType(OrderLogConsts.ORDER_LOG_TYPE_MASTER_MODIFY_DUTY_TIME);
//            sendMessageRespDTO.setOrderId(queueOrderNodeNotifyDTO.getOrderId());
//            sendMessageRespDTO.setWorkId(queueOrderNodeNotifyDTO.getWorkId());
//            sendMessageRespDTO.setPlat(Optional.ofNullable(queueOrderNodeNotifyDTO.getPlatWork()).orElse(queueOrderNodeNotifyDTO.getPlat()));
//            boolean isSendMessage = sendOrderMessageService.sendOrderMessage(sendMessageRespDTO);
//
//            logger.info("#oms#message#orderId={} task 发送订单ruleId={}消息结果：{}", orderId, sendMessageRespDTO.getNodeMark(), isSendMessage);
//
//        } catch (Exception ex) {
//            logger.error(String.format("#oms#message#orderId=%s，task 发送订单消息结果失败:%s", orderId, ex.getMessage()), ex);
//        }
//    }
}
